﻿namespace YunQue.WebApi.Extensions
{
    /// <summary>
    /// jwt扩展
    /// </summary>
    public static class JwtExtension
    {
        /// <summary>
        /// 添加Jwt配置
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        public static IServiceCollection AddJwtConfig(this IServiceCollection service)
        {
            service.AddAuthentication(options =>
            {
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettingManager.JWT_SECRET)),
                    ValidateIssuer = true,
                    ValidIssuer = AppSettingManager.JWT_ISSUER,
                    ValidateAudience = true,
                    ValidAudience = AppSettingManager.JWT_AUD,
                    ValidateLifetime = true,
                    ClockSkew = TimeSpan.Zero,
                    RequireExpirationTime = true,
                };

            });
            return service;
        }

    }
}
